<template>
  <div class="hm-input" :class="{'hm-input--suffix':showSuffix}">
    <!-- 如果传了showPassword，判断是否需要切换密码的显示
         如果不传，不判断，按照传进来输入框类型设置 
    -->
    <input
      :placeholder="placeholder"
      :type="showPassword ? (passwordVisib ?  'text':'password'):type"
      class="hm-input__inner"
      :name="name"
      :disabled="disabled"
      :value="value"
      @input="handleInput"
      :class="{'is-disabled':disabled}"
    />
    <span class="hm-input__suffix" v-if="showSuffix">
      <i class="hm-input__icon hm-icon-circle-close" v-if="clearable && value" @click="clear"></i>
      <i
        class="hm-input__icon hm-icon-view"
        v-if="showPassword"
        @click="togglePassword"
        :class="{'hm-icon-view-active':passwordVisib}"
      ></i>
    </span>
  </div>
</template>

<script>
export default {
  name: "hmInput",
  data() {
    return {
      passwordVisib: false,
    };
  },
  props: {
    placeholder: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "text",
    },
    name: {
      type: String,
      default: "usernmae",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    value: {
      type: String,
      default: "",
    },
    clearable: {
      type: Boolean,
      default: false,
    },
    showPassword: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    handleInput(e) {
      this.$emit("input", e.target.value);
    },
    clear() {
      //把内容清空
      this.$emit("input", "");
    },
    //传入进来的属性是不能直接修改的，那么可以通过定义一个data属性来修改行了
    togglePassword() {
      console.log(1)
      this.passwordVisib = !this.passwordVisib;
    },
  },
  computed: {
    showSuffix() {
      return this.clearable || this.showPassword;
    },
  },
};
</script>

<style lang="scss">
.hm-input {
  width: 100%;
  position: relative;
  font-size: 14px;
  display: inline-block;
  .hm-input__inner {
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    outline: none;
    padding: 0 15px;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    width: 100%;

    &:focus {
      outline: none;
      border-color: #409eff;
    }
    &.is-disabled {
      background-color: #f5f7fa;
      border-color: #e4e7ed;
      color: #c0c4cc;
      cursor: not-allowed;
    }
  }
}

.hm-input--suffix {
  .hm-input__inner {
    padding-right: 30px;
  }
  .hm-input__suffix {
    position: absolute;
    height: 100%;
    right: 10px;
    top: 0;
    line-height: 40px;
    text-align: center;
    color: #c0c4cc;
    transition: all 0.3s;
    z-index: 900;
    i {
      color: #c0c4cc;
      font-size: 14px;
      cursor: pointer;
      transition: color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    }
    .hm-icon-view-active {
      color: blue;
    }
  }
}
</style>>